package com.springboot.wisdom.mqtt.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.springboot.wisdom.common.common.ResultUtil;
import com.springboot.wisdom.common.common.bean.MyResult;
import com.springboot.wisdom.common.common.bean.PageQueryDTO;
import com.springboot.wisdom.common.util.UserUtils;
import com.springboot.wisdom.mqtt.mapper.HardwareDeviceMapper;
import com.springboot.wisdom.mqtt.service.HardwareDeviceService;
import com.springboot.wisdom.serviceApi.mqtt.model.dto.HardwareDeviceDTO;
import com.springboot.wisdom.serviceApi.mqtt.model.entity.HardwareDeviceDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 10236
 * @description 针对表【hardware_device(智能设备表)】的数据库操作Service实现
 * @createDate 2025-04-24 18:05:07
 */
@Service
public class HardwareDeviceServiceImpl extends MPJBaseServiceImpl<HardwareDeviceMapper, HardwareDeviceDO>
        implements HardwareDeviceService {

    @Autowired
    private UserUtils userUtils;

    @Autowired
    private HardwareDeviceMapper hardwareDeviceMapper;

    @Override
    public MyResult<Page<HardwareDeviceDTO>> getInfoPageBy(PageQueryDTO pageQueryDTO, HardwareDeviceDTO vo) {
        String userId = userUtils.getLoginUserId();
        System.out.println("userId = " + userId);

        // 连表求总数。s
        Long total = hardwareDeviceMapper.selectJoinCount(new MPJQueryWrapper<>());

        // 连表分页查询。
        MPJLambdaWrapper<HardwareDeviceDO> lambdaWrapper = new MPJLambdaWrapper<HardwareDeviceDO>()
                .selectAll(HardwareDeviceDO.class)
                .orderByDesc(HardwareDeviceDO::getUpdateTime);

        Page<HardwareDeviceDTO> logVOIPage = hardwareDeviceMapper.selectJoinPage(
                pageQueryDTO.toPage(pageQueryDTO.getCurrent(), pageQueryDTO.getSize(), total, false)
                , HardwareDeviceDTO.class, lambdaWrapper);
        System.out.println(logVOIPage.getRecords());//获取分页记录
        System.out.println(logVOIPage.getPages());//总页数
        System.out.println(logVOIPage.getTotal());//总记录数
        System.out.println(logVOIPage.hasPrevious());//是否有上一页
        System.out.println(logVOIPage.hasNext());//是否有下一页
        return ResultUtil.success(logVOIPage);
    }

    @Override
    public MyResult<String> add(HardwareDeviceDTO vo) {
        vo.setCreateTime(null);
        vo.setUpdateTime(DateTime.now());
        boolean result = hardwareDeviceMapper.insertOrUpdate(vo);
        return result ? ResultUtil.success("添加成功") : ResultUtil.error("添加失败");
    }

    public List<HardwareDeviceDTO> getInfoConnectBy(String id) {
        hardwareDeviceMapper.selectJoinList(HardwareDeviceDTO.class, new MPJLambdaQueryWrapper<HardwareDeviceDO>()
                .selectAll(HardwareDeviceDO.class)
                .eq(HardwareDeviceDO::getDeviceId, id)
                .or()
                .eq(HardwareDeviceDO::getDeviceFromId, id)
                .orderByAsc(HardwareDeviceDO::getDeviceId)
        );
        return null;
    }
}
